//
//  DownLoadViewController.m
//  网络FM
//
//  Created by dengzhicheng on 15/5/26.
//  Copyright (c) 2015年 dengzhicheng.com. All rights reserved.
//

#import "DownLoadViewController.h"
#import "VOSegmentedControl.h"

@interface DownLoadViewController ()<UIScrollViewDelegate>

@property (nonatomic, retain)UIScrollView *scroll;
@property (nonatomic, retain)VOSegmentedControl *segment;
@property (nonatomic, retain)UIView *aView;
@property (nonatomic, retain)UIView *bView;

@end

@implementation DownLoadViewController

- (void)dealloc
{
    [_aView release];
    [_bView release];
    [_scroll release];
    [_segment release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)loadView
{
    [super loadView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createView];
    [self createSegment];
    [self createScroll];
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
}

- (void)createView
{
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 20, 50, 30)];
    [backButton setBackgroundColor:[UIColor clearColor]];
    [backButton setTitle:@"返回" forState:UIControlStateNormal];
    [backButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
    [backButton release];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 80) / 2, 20, 80, 30)];
    titleLabel.text = @"我的下载";
    titleLabel.font = [UIFont boldSystemFontOfSize:17];
    [self.view addSubview:titleLabel];
    [titleLabel release];
    

}

- (void)createSegment
{
    self.segment = [[VOSegmentedControl alloc] initWithSegments:@[@{VOSegmentText:@"已下载"},@{VOSegmentText:@"正在下载"}]];
    _segment.frame = CGRectMake((self.view.frame.size.width - 180) / 2, 50, 180, 20);
    _segment.indicatorStyle = VOSegCtrlIndicatorStyleTopLine;
    _segment.indicatorThickness = 0;
    [_segment addTarget:self action:@selector(segmentClicked:) forControlEvents:(UIControlEventValueChanged)];
    [self.view addSubview:_segment];
    [_segment release];
}

- (void)createScroll
{
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 70, self.view.frame.size.width, self.view.frame.size.height - 70)];
    [self.scroll setContentSize:CGSizeMake(self.view.frame.size.width * 2, 0)];
    self.scroll.delegate = self;
    [self.scroll setPagingEnabled:YES];
    self.scroll.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:_scroll];
    [_scroll release];
    
    self.aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.scroll.frame.size.width, self.scroll.frame.size.height)];
    [_aView setBackgroundColor:[UIColor orangeColor]];
    [self.scroll addSubview:_aView];
    [_aView release];
    
    self.bView = [[UIView alloc] initWithFrame:CGRectMake(self.scroll.frame.size.width, 0, self.scroll.frame.size.width, self.scroll.frame.size.height)];
    [_bView setBackgroundColor:[UIColor lightGrayColor]];
    [self.scroll addSubview:_bView];
    [_bView release];
}

#pragma mark -
#pragma mark scrollView的偏移结束时与segmentedControl关联
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x == 0) {
        self.segment.selectedSegmentIndex = 0;
    } else if (scrollView.contentOffset.x == self.view.frame.size.width) {
        self.segment.selectedSegmentIndex = 1;
    }
}
#pragma mark -
#pragma mark 点击segmentedControl,scrollCiew偏移
- (void)segmentClicked:(UISegmentedControl *)seg
{
    switch (seg.selectedSegmentIndex) {
        case 0:
        {
            [self.scroll setContentOffset:CGPointMake(0, 0)];
        }
            break;
        case 1:
        {
            [self.scroll setContentOffset:CGPointMake(self.view.frame.size.width, 0)];
        }
            break;
        default:
            break;
    }
}


- (void)backButtonClick:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
